package erfgame.core.graph.simple;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class Node<ValueType extends Serializable> implements Serializable {
	private Collection<Node<ValueType>> connections;
	private int incomingConnections;
	private ValueType value;
	
	public Node( ValueType value ) {
		this.connections = new HashSet<Node<ValueType>>(4);
		this.value = value;
	}
	
	public ValueType getValue() {
		return this.value;
	}
	
	public void addConnection( Node<ValueType> node ) {
		if( this.connections.add( node ) ) {
			node.incomingConnections++;			
		}
	}
	
	public void removeConnection( Node<ValueType> node ) {
		if( this.connections.remove( node ) ) {
			node.incomingConnections--;
		}
	}
	
	public Collection<Node<ValueType>> getConnections() {
		return this.connections;
	}
	
	public int getIncomingConnections() {
		return this.incomingConnections;
	}

	@Override
	@SuppressWarnings(value="unchecked")
	public boolean equals(Object obj) {
		if( obj != null && obj.getClass().equals( this.getClass() ) ) {
			return equals( (Node<ValueType>)obj );
		} else {
			return false;
		}
	}
	
	public boolean equals( Node<ValueType> node ) {
		return node.getValue().equals( this.getValue() );
	}

	@Override
	public int hashCode() {
		return value.hashCode();
	}
}
